package Tests.ext
{
	import Box2D.Collision.Shapes.b2CircleShape;
	import Box2D.Collision.Shapes.b2PolygonShape;
	import Box2D.Common.Math.b2Vec2;
	import Box2D.Dynamics.b2Body;
	import Box2D.Dynamics.b2BodyDef;
	import Box2D.Dynamics.b2BodyType;
	import Box2D.Dynamics.b2FixtureDef;
	
	import Framework.Test;
	
	import flash.system.System;
	
	public class CCD extends Test
	{
		public function CCD()
		{
			super();
			createGround();
			createChair();
			createBullets();
		}
		private function createGround():void{
			m_groundBody.SetTransform(new b2Vec2(0,20),0);
			
			var shape:b2PolygonShape = new b2PolygonShape;
			var fd:b2FixtureDef = new b2FixtureDef;
			fd.shape = shape;
			fd.density = 0;
			fd.restitution = 1.4;
			
			shape.SetAsBoxByCenter(.1,10,new b2Vec2(-10,0),0);
			m_groundBody.CreateFixture(fd);
			
			shape.SetAsBoxByCenter(.1,10,new b2Vec2(10,0),0);
			m_groundBody.CreateFixture(fd);
			
			shape.SetAsBoxByCenter(.1,10,new b2Vec2(0,-10),.5*Math.PI);
			m_groundBody.CreateFixture(fd);
			
			shape.SetAsBoxByCenter(.1,10,new b2Vec2(0,10),.5*Math.PI);
			m_groundBody.CreateFixture(fd);						
		}
		private function createChair():void{
			var bd:b2BodyDef = new b2BodyDef;
			bd.type = b2BodyType.b2_dynamicBody;
			bd.position.Set(0,15);
			var body:b2Body = m_world.CreateBody(bd);
			
			var shape:b2PolygonShape = new b2PolygonShape;
			var fd:b2FixtureDef = new b2FixtureDef;
			fd.density = 4;
			fd.shape = shape;
			
			shape.SetAsBox(1.5,.15);
			body.CreateFixture(fd);
			
			shape.SetAsBoxByCenter(.15,2.7,new b2Vec2(-1.45,2.35),.2);
			body.CreateFixture(fd);
			
			shape.SetAsBoxByCenter(.15,2.7,new b2Vec2(1.45,2.35),-.2);
			body.CreateFixture(fd);
		}
		private function createBullets():void{
			for(var i:int;i<10;i++){
				var bd:b2BodyDef = new b2BodyDef;
				bd.type = b2BodyType.b2_dynamicBody;
				bd.position.Set(0,15+i);
				bd.angularVelocity = RandomFloat(-50,50);
				var body:b2Body = m_world.CreateBody(bd);
				var shape:b2CircleShape = new b2CircleShape;
				shape.radius = .25;
				var fd:b2FixtureDef = new b2FixtureDef;
				fd.shape = shape;
				fd.density = 1;
				fd.restitution = 1;
				body.CreateFixture(fd);
			}
		}
		public override function Step():void{
			writeMessage('continuous collision detection');
			super.Step();
		}
	}
}